<template>
	<view class="content">
		<view class="pageHeader">
			我的地址
			<a class="generalBack" @click="backpage()" hover-class="none">
				<image src="/static/b-1.png" style="width: 18upx;height: 30upx;"></image>
			</a>
		</view>
		<view class="regoodbox">

			<radio-group>
				<view class="list b-b" style="background-color: #fff;padding: 20upx 30upx;font-size: 30upx;color: #666;margin-bottom: 20upx;"
				 v-for="(item, index) in addressList" :key="index" @click="chooseit(item)">
					<view class="flex aic jsb">
						<view> {{item.name}}</view>
						<label @click.stop="radchange(item.id,item)">
							<radio color="#EC6104" :checked="item.is_default == 1" style="transform:scale(0.8)" />
							<text class="tag">默认地址</text>
						</label>
					</view>
					<view style="margin: 12upx 0;">{{item.tel}}</view>
					<view style="font-size: 26upx;border-bottom: 1px solid #eee;padding-bottom: 10upx;">
						{{item.province_name}} {{item.city_name}}{{item.county_name}}{{item.address}}
					</view>
					<view class="flex jc-end" style="padding-top: 12upx;font-size: 26upx;">
						<view class="flex aic" @click.stop="editaddress(item)">
							<image src="../../../static/add-1.png" style="width: 28upx;height: 28upx;margin-right: 6upx;"></image>
							<text>编辑</text>
						</view>
						<view class="flex aic" style="margin-left: 50upx;" @click.stop="deleteAddress(item.id,index)">
							<image src="../../../static/add-2.png" style="width: 28upx;height: 28upx;margin-right: 4upx;"></image>
							<text>删除</text>
						</view>
					</view>


					<!-- <text class="yticon" @click.stop="addAddress2('edit', item)">编辑</text>
							<text @click.stop="deladdress(index,item)">删除</text> -->
				</view>
			</radio-group>
			
			<view style="text-align: center;margin-top:50upx" v-if="tips">
				<image style="width:340upx;height:328upx;" src="../../../static/empty.png" mode=""></image>
				<text style="display:block;color:#999999;font-size: 30upx;">你还没有收货地址哦~</text>
			</view>

		</view>
		
		
		<view class="add_dizhi" @click="addresAdd">
			添加新地址
		</view>
		
	</view>
</template>
<script>
	import {  
		mapState,  
		mapMutations
	} from 'vuex'; 
	import postAjax from '../../../API/postAjax.js'
	export default {
		data(){
			return {
				userToken: '', // token
				// 地址数据是否为空
				tips: false,
				type:'',
				entertype:'',
				addressList: []
			}
		},
		onLoad(options){
			let that = this
			that.type = options.type
			if(options.entertype){
				that.entertype = options.entertype
			}
			that.userToken = uni.getStorageSync('token')
			that.getAddressList()
		},
		methods: {
			...mapMutations(['myaddress']),
			// 返回上一页
			backpage() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 地址选择 不同类型（订单地址选择、自动设置选择）
			chooseit(i){
				if(this.type =='autobuy'||this.type =='tranbuy'||this.type =='comfirmOrder'){
					var pages = getCurrentPages();
					var prepage = pages[pages.length - 2]; //上一个页面
					prepage.$vm.myaddress = i
					uni.navigateBack({
						delta:1
					})
				}
				if(this.entertype =='autobuy'||this.entertype =='tranbuy'||this.entertype =='comfirmOrder'){
					var pages = getCurrentPages();
					var prepage = pages[pages.length - 4]; //上一个页面
					prepage.$vm.myaddress = i
					uni.navigateBack({
						delta:3
					})
				}
			},
			// 修改默认地址
			radchange(id,item) {
				let that = this
				postAjax('UserAddress/SetDefault', {token:this.userToken,id:id}, function(data) {
					console.log(data)
					if (data.code == 0) {
						uni.showToast({
							title: "设置成功",						
						})
						that.myaddress(item)
					}else{
						uni.showToast({
							title: "设置失败",
							icon: 'none'
						})
					}
				})
			},
			// 添加地址
			addresAdd() {
				let that = this
				let linkUrl = 'add?type=1'
				console.log(that.type)
				if(that.type =='autobuy'||that.type =='tranbuy'||that.type =='comfirmOrder'){
					uni.navigateTo({
						url: linkUrl+'&entertype='+that.type
					})
				}else{
					uni.navigateTo({
						url: linkUrl
					})
				}
				
			},
			// 进入编辑页
			editaddress(item) {
				let that = this
				let entertype = ''
				if(that.type =='autobuy'||that.type =='tranbuy'||that.type =='comfirmOrder'){
					entertype = that.type
				}else{
					entertype = ''
				}
				uni.navigateTo({
					url: `add?data=${JSON.stringify(item)}&type=2&entertype=${entertype}`
				})
			},
			
			deleteAddress(id, index) {
				let that = this
				let postData = {
					token:that.userToken,
					id: id
				}
				uni.showModal({
					title: '提示',
					content: '是否确认删除？',
					success: function(res) {
						if (res.confirm) {							
							postAjax('UserAddress/Delete', postData, function(data) {
																						
								if (data.code == 0) {
									that.addressList.splice(index, 1)
									uni.showToast({
										title: data.msg
									})
									
									if (that.addressList.length == 0) {
										that.tips = true
									}
								} else {
									uni.showToast({
										icon: 'none',
										title: data.msg
									})
								}
							})
						}
					}
				});

			},
			
			// 获取地址信息
			getAddressList() {
				let that = this
				postAjax('UserAddress/Index', {token:that.userToken}, function(data) {
					console.log(data)
					if (data.code == 0) {
						if (data.data.length > 0) {
							that.addressList = data.data
							// 保存默认地址到本地 
							let defaultAddress = that.addressList.filter(item=>item.is_default==1)
							that.myaddress(defaultAddress)
						} else {
							that.tips=true
						}
					}else{
						that.getAddressList()
					}

				})

			}
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
		background-color: #F9F9F9;
	}

	.pageHeader {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		width: 100%;
		height: 128upx;
		background-color: #1D84E8;
		line-height: 128upx;
		text-align: center;
		font-size: 36upx;
		color: #fff;
		padding-top: 40upx;
	}

	.generalBack {
		position: absolute;
		top: 40upx;
		left: 30upx;
		display: block;
	}

	.rebtn {
		display: inline-block;
		font-size: 30upx;
		border-radius: 4upx;
		width: 120upx;
		height: 58upx;
		line-height: 58upx;
	}

	.rebtn::after {
		border: none;
	}

	.regoodbox {
		padding: 170upx 0upx 100upx;
	}

	.add_dizhi {
		position: fixed;
		z-index: 99;
		width: 100%;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		background-color: #1D84E8;
		font-size: 32upx;
		color: #fff;
		bottom: 0;
		left: 0;
	}
</style>
